package city.spring.service.estimate.impl;

import city.spring.domain.entity.estimate.EstimateOptionEntity;
import city.spring.domain.entity.estimate.EstimateProjectEntity;
import city.spring.repository.estimate.EstimateProjectRepository;
import city.spring.service.estimate.EstimateOptionService;
import city.spring.service.estimate.EstimateProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Throwable.class)
public class EstimateProjectServiceImpl extends ServiceImpl<EstimateProjectRepository, EstimateProjectEntity> implements EstimateProjectService {

    private EstimateOptionService estimateOptionService;

    public EstimateProjectServiceImpl(EstimateOptionService estimateOptionService) {
        this.estimateOptionService = estimateOptionService;
    }

    @Override
    public void loadOptions(EstimateProjectEntity entity) {
        entity.setEstimateOptionEntities(this.estimateOptionService.getOptionsByProject(entity.getId()));
    }

    @Override
    public Map<String, List<EstimateOptionEntity>> itemOptions(String projectId) {
        List<EstimateOptionEntity> estimateOptionEntities = this.estimateOptionService.getOptionsByProject(projectId);
        List<List<EstimateOptionEntity>> options = new LinkedList<>();
        estimateOptionEntities.stream().collect(Collectors.groupingBy(EstimateOptionEntity::getItem, Collectors.toList()))
                .forEach((item, estimateOptionsByItems) -> {
                    options.add(estimateOptionsByItems);
                });
        Map<String, List<EstimateOptionEntity>> newMap = new HashMap<>();
        for (int i = 0; i < options.size(); i++) {
            if (options.get(i).get(0).getQuestion() != null) {
                newMap.put(options.get(i).get(0).getItem() + "  ：  " + options.get(i).get(0).getQuestion(), options.get(i));
            } else {
                newMap.put(options.get(i).get(0).getItem(), options.get(i));
            }
        }
        return newMap;
    }

    @Override
    public List<EstimateProjectEntity> getEstimateProject() {
        return baseMapper.getEstimateProjectName();
    }

    @Override
    public void computePoints2(List<String> optionList, EstimateProjectEntity entity) {
        List<String> projectOptionIds = new LinkedList<>();
        int points = 0;
        for (int i = 0; i < optionList.size(); i++) {
            EstimateOptionEntity estimateOptionEntity = estimateOptionService.getById(optionList.get(i));
            if (estimateOptionEntity.getProjectId().equals(entity.getId())) {
                points += estimateOptionService.getById(optionList.get(i)).getPoints();
            }
        }
        entity.setPoints(points);
    }

}
